//index.js
//获取应用实例
import api from '../../utils/api.js'
const {request} = require('../../utils/wx-api.js')
// console.log(request)
const app = getApp()

Page({
  data: {
    categoryList:"",
    topicList:"",
    brandList:"",
    hotGoodsList:"",
    newGoodsList:"",
    channel: "",
    img: [
        "https://miss-tree.gitee.io/web-pic/images/jiaju.png",
        "https://miss-tree.gitee.io/web-pic/images/canchu.png",
        "https://miss-tree.gitee.io/web-pic/images/tongji.png",
        "https://miss-tree.gitee.io/web-pic/images/shizhuang.png",
        "https://miss-tree.gitee.io/web-pic/images/xingqu.png",
    ],
    scrollTop: 0,
    banner: "",
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000
  },
  newWeb(e){
    // console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "../details/detail?id="+id
    })
  },
  onLoad(options) {

    console.log(options)
  },
  //事件处理函数
  onShow() {
    let that = this;
    let indexData = wx.getStorageSync("indexData")
    if (indexData) {
      // 缓存数据
      // wx.setStorage({
      //   key: 'indexData',
      //   data: '',
      // })
      // console.log(indexData)
      let banner = indexData.banner;
      let channel = indexData.channel;
      let newGoodsList = indexData.newGoodsList;
      let hotGoodsList = indexData.hotGoodsList;
      let brandList = indexData.brandList;
      let topicList = indexData.topicList;
      let categoryList = indexData.categoryList;
      that.setData({
        banner,
        channel,
        newGoodsList,
        hotGoodsList,
        brandList,
        topicList,
        categoryList
      })
    } else {
      // wx.request({
      //   url: api.indexList,
      //   success: function(res) {
      //     // console.log(res)
      //     // wx.setStorageSync("indexData",res.data.data)
      //     let banner = res.data.data.banner;
      //     let channel = res.data.data.channel;
      //     let newGoodsList = res.data.data.newGoodsList;
      //     let hotGoodsList = res.data.data.hotGoodsList;
      //     let brandList = res.data.data.brandList;
      //     let topicList = res.data.data.topicList;
      //     let categoryList = res.data.data.categoryList;
      //     that.setData({
      //       banner,
      //       channel,
      //       newGoodsList,
      //       hotGoodsList,
      //       brandList,
      //       topicList,
      //       categoryList
      //     })
      //   }
      // })
      request({
          url: api.indexList
        })
        .then(res => {
          console.log(res);
          wx.setStorageSync("indexData", res.data.data)
          
        })
    }
  },
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})